package com.wlr.study.leetcode;

import java.util.Arrays;

/**
 * @author lerong.wang
 * @version 1.0.0
 * @description
 * @date 2025/4/14 18:06
 */
public class LeetCode31 {
    public void nextPermutation(int[] nums) {
        int index = 0;
        for (int j = nums.length - 1; j > 0; j--) {
            if (nums[j] > nums[j - 1]) {
                for (int k = nums.length - 1; k > j - 1; k--) {
                    if (nums[k] > nums[j - 1]) {
                        int temp = nums[k];
                        nums[k] = nums[j - 1];
                        nums[j - 1] = temp;
                        index = j;
                        break;
                    }
                }
                break;
            }
        }
        Arrays.sort(nums, index, nums.length);
        System.out.println(Arrays.toString(nums));
    }

    public static void main(String[] args) {
        LeetCode31 leetcode31 = new LeetCode31();
        leetcode31.nextPermutation(new int[]{4,5,2,6,3,1});
    }

}